import { makeObservable, observable, action, computed } from "mobx";
class AppleItem {
  constructor() {
    makeObservable(this);
  }

  @observable appleList = [];
  @observable alreadyAppleList = [];
  @observable number = 1;

  @action addApple = () => {
    this.appleList.push({
      number: this.number,
      weight: Math.ceil(Math.random() * 100 + 200),
    });
    this.number++;
  };

  @computed get currentApples() {
    return {
      number: this.appleList.length,
      weight: this.appleList.reduce((sum, apple) => {
        return sum + apple.weight;
      }, 0),
    };
  }

  @action eatApple = (number) => {
    const index = this.appleList.findIndex((v) => (v.number = number));
    this.alreadyAppleList = [
      ...this.alreadyAppleList,
      ...this.appleList.splice(index, 1),
    ];
  };

  @computed get alreadyAppleListData() {
    return {
      number: this.alreadyAppleList.length,
      weight: this.alreadyAppleList.reduce((sum, apple) => {
        return sum + apple.weight;
      }, 0),
    };
  }
}

const apple = new AppleItem();
export default apple;
